using System.Windows.Forms;

namespace 图片坐标颜色
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string filePath = "";
        private void textBox1_Click(object sender, EventArgs e)
        {
            //创建文件弹出选择窗口（包括文件名）对象
            OpenFileDialog ofd = new OpenFileDialog();
            //判断选择的路径
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName.ToString();
                pictureBox1.Image = Image.FromFile(ofd.FileName.ToString());

            }
            filePath = textBox1.Text;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
                return;
            MouseEventArgs me = (MouseEventArgs)e;
            //Bitmap b = new Bitmap(pictureBox1.Image);
            //MessageBox.Show("X=" + (1024 / 800) * me.X + ", Y=" + (768 / 600) * me.Y);
            //b.GetPixel((1024 / 800) * me.X, (768 / 600) * me.Y);
            // 获取 PictureBox 和图片的尺寸
            Image b = pictureBox1.Image;
            int pbWidth = pictureBox1.ClientSize.Width;
            int pbHeight = pictureBox1.ClientSize.Height;
            int imgWidth = b.Width;
            int imgHeight = b.Height;

            // 计算图片显示区域的宽度和高度
            float imgAspectRatio = (float)imgWidth / imgHeight;
            float pbAspectRatio = (float)pbWidth / pbHeight;
            int displayWidth, displayHeight;
            int offsetX = 0, offsetY = 0;

            if (imgAspectRatio > pbAspectRatio)
            {
                displayWidth = pbWidth;
                displayHeight = (int)(pbWidth / imgAspectRatio);
                offsetY = (pbHeight - displayHeight) / 2;
            }
            else
            {
                displayWidth = (int)(pbHeight * imgAspectRatio);
                displayHeight = pbHeight;
                offsetX = (pbWidth - displayWidth) / 2;  
            }

            // 将点击位置转换为图片坐标
            int x = (int)((me.X - offsetX) * imgWidth / displayWidth);
            int y = (int)((me.Y - offsetY) * imgHeight / displayHeight);

            //Image b = pictureBox1.Image;
            //int x = b.Width * me.X / pictureBox1.Width;
            //int y = b.Height * me.Y / pictureBox1.Height;
            Bitmap bmp = new Bitmap(pictureBox1.Image);
            Color cl= bmp.GetPixel(x, y);

            //var img = pictureBox1.Image;
            //float ratioX = (float)img.Width / pictureBox1.ClientSize.Width;
            //float ratioY = (float)img.Height / pictureBox1.ClientSize.Height;
            //int x = (int)(me.X * ratioX);
            //int y = (int)(me.Y * ratioY);

            //// Get the pixel color at the click position
            //var bitmap = new Bitmap(img);
            //var cl = bitmap.GetPixel(x, y);

            button1.BackColor= cl;
            //MessageBox.Show(String.Format($"X={x}, Y={y} 颜色：" + cl.R + "," + cl.G + "," + cl.B));
            label2.Text = String.Format($"X={x}, Y={y} 颜色：" + cl.R + "," + cl.G + "," + cl.B);


           
        }
    }
}